﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using Wicture.DbRESTFul.SQL;
using Wicture.EPC.Common;
using Wicture.EPC.Service.V1.Models.LiYang;
using Wicture.EPC.Service.V1.Repository;

namespace Wicture.EPC.Service.V1.QueryInterceptors.Interceptors
{
    public class TeslaQueryInterceptor : BaseQueryInterceptor
    {
        private readonly string[] brandCodes = new string[] { "tesla" };
        public override string[] BrandCodes => brandCodes;

        protected override List<Query_PartsResult> GetParts(DbRESTFulRepository repository, JObject param)
        {
            return repository.Invoke<Query_PartsResult>(CSIs.Query_Parts, param);
        }

        public override object QueryPartDetail(DbRESTFulRepository repository, JObject param)
        {
            param.RemoveEmptyValues();
            var brandCode = param.Value<string>(ParamFields._brandCode);
            var partId = param.Value<string>(ParamFields._partId);
            var partCode = param.Value<string>(ParamFields._partCode);
            dynamic data = null;

            if (string.IsNullOrEmpty(partId))
            {
                data = repository.Invoke(CSIs.Get_Part_Detail_Standard, new { partCode, brandCode });
            }
            else
            {
                data = repository.Invoke(CSIs.Get_Usage_Detail_Standard, param);
            }
            if (data == null) return null;
            // 1、基本信息赋值
            Query_Part_DetailResult result = JObject.FromObject(data).ToObject<Query_Part_DetailResult>();


            //4.配件附加属性处理
            var props = new List<Query_Part_DetailResult._prop>
            {
                new Query_Part_DetailResult._prop { key = "件数", value = result.quantity ?? string.Empty },
            };
            result.props = props.Where(q => !string.IsNullOrEmpty(q.value)).ToList();
            return result;
        }
        /// <summary>
        /// 根据力洋车型获取epc车型
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public override object ModelSelectorByLiYang(DbRESTFulRepository repository, JObject param)
        {
            Get_Model_InfoResult result = null;
            LiYangInfo info = param.ToObject<LiYangInfo>();

            string vin = info.vin;
            var liyangVinInfo = repository.Invoke(CSIs.Get_LiYang_Vin, new { vin });
            if (liyangVinInfo != null)
            {
                LiYangInfo liyangData = JsonConvert.DeserializeObject<LiYangInfo>(liyangVinInfo.liyangData);
                liyangData.vin = info.vin;
                liyangData.selectorId = info.selectorId;
                info = liyangData;
            }

            List<Get_Model_InfoResult> list = repository.Invoke<Get_Model_InfoResult>(CSIs.Query_Model_By_LiYang_For_Tesla, info);
            if (list != null && list.Any())
            {
                result = list[0];
                result.vin = vin;
                result.warning = "注意：此车架号只解析到车型，请仔细核对";
                result.props = GetProps(info, result.name);
            }
            return result;
        }
        private List<Get_Model_InfoResult._props> GetProps(LiYangInfo param, string epcModelName)
        {
            List<Get_Model_InfoResult._props> features = new List<Get_Model_InfoResult._props>();
            features.Add(new Get_Model_InfoResult._props { key = "车型", value = epcModelName });
            features.Add(new Get_Model_InfoResult._props { key = "年款", value = param.Year });
            features.Add(new Get_Model_InfoResult._props { key = "发动机型号", value = param.EngineModel });
            features.Add(new Get_Model_InfoResult._props { key = "驱动", value = param.DriveMode });
            return features;
        }

        protected override List<Query_SubGroupResult> FilterSubGroupsByVin(DbRESTFulRepository repository, List<Query_SubGroupResult> results, JObject param)
        {
            return results;
        }
        protected override void FilterPartsByVin(DbRESTFulRepository repository, ref List<Query_PartsResult> result, string vin, string selectorId)
        {
        }
    }
}
